/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.GasCouponsDao;
import com.gmail.blackdog1987.carcare.dao.PaylogDao;
import com.gmail.blackdog1987.carcare.dao.ProductDao;
import com.gmail.blackdog1987.carcare.dao.SmsDao;
import com.gmail.blackdog1987.carcare.dao.UserDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TGasCoupons;
import com.gmail.blackdog1987.carcare.entity.TPaylog;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.entity.TSms;
import com.gmail.blackdog1987.carcare.entity.TUser;
import com.gmail.blackdog1987.carcare.entity.base.Page;
import com.gmail.blackdog1987.carcare.service.ProductService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;

/**
 * 
 * @author 董国超
 *
 */
@Service("productServiceImpl")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;
	@Autowired
	private BaseJdbcDao baseJdbcDao;
	@Autowired
	private PaylogDao paylogDao;
	@Autowired
	private SmsDao smsDao;
	@Autowired
	private GasCouponsDao gasCouponsDao;
	@Autowired
	private UserDao userDao;
	
	@Override
	public Map<String, Object> doPay(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String price = filter.get("price");
		String userid = filter.get("userid");
		String table = filter.get("table");
		String table_id = filter.get("table_id");
		
		String paylog = "doPay : price " + price + " userid "+ userid + " tableName " + table + " tableId " + table_id;
		
		TPaylog log = new TPaylog();
		log.setCreatetime(new Date());
		log.setInfo(paylog);
		log.setProductid(0);
		paylogDao.save(log);
		
		System.out.println(paylog);
		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("price", Double.valueOf(price));
		params.put("tableName", table);
		params.put("tableId", Integer.valueOf(table_id));
		params.put("userid", Integer.valueOf(userid));
//		params.put("status!=", 1);
		List<TProduct> products = productDao.find(params);
		if(products==null||products.size()==0){
			result.put("code", ErrorCode.ORDER_PAY);
			return result;
		}
		boolean isPaid = false;   // 去重，已经支付成功的，再回调，不再发短信
		for (TProduct tProduct : products) {
			if(tProduct.getStatus()==1){
				isPaid = true;
			}
			tProduct.setStatus(1);
			tProduct.setFinishtime(new Date());
		}
		
		TUser user = userDao.get(Integer.valueOf(userid));
		
		String license = "";
		int i = products.get(0).getBody().indexOf("车牌号");
		if(i!=-1){
			license = products.get(0).getBody().substring(i+3);
		}
		
		
		if(!isPaid){
			TGasCoupons coupons = new TGasCoupons();
			coupons.setCode(System.currentTimeMillis()+"");
			Calendar c =  Calendar.getInstance();
			coupons.setCreatetime(new Date());
			coupons.setLicense(license);
			coupons.setStarttime(new Date());
			c.add(Calendar.YEAR, 1);
			coupons.setEndtime(new Date(c.getTime().getTime()));
			coupons.setStatus(0);
			coupons.setPrice(Double.valueOf(price));
			coupons.setInfo(productDao.type.get(table)==null?"商品":productDao.type.get(table) +", 用户电话:"+user.getUsername());
			coupons.setUserid(Integer.valueOf(userid));
			gasCouponsDao.save(coupons);
			
			// 发短信
			TSms sms = new TSms();
			sms.setContent("恭喜您下单成功，请携带本验证码到店使用:[" + coupons.getCode() + "]价值"+price+"元");
			if(smsDao.findBy("content", "恭喜您下单成功，请携带本验证码到店使用:[" + coupons.getCode() + "]价值"+price+"元").size()==0){
				sms.setCreatetime(new Date());
				sms.setFailuretimes(0);
				sms.setPhone(user.getUsername());
				sms.setUpdatetime(new Date());
				sms.setStatus(0);
				smsDao.save(sms);
			}
		}
		
		productDao.saveAll(products);
		return result;
	}
	
	@Override
	public Map<String, Object> getList(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", ErrorCode.NO);
		String pageindex = filter.get("pageindex");
		String pagesize = filter.get("pagesize");
		String userid = filter.get("userid");
		int index = 1;
		int size = 20;
		if(pageindex!=null&&pageindex.length()!=0){
			index = Integer.valueOf(pageindex);
		}
		
		if(index<1){
			index = 1;
		}
		if(pagesize!=null&&pagesize.length()!=0){
			size = Integer.valueOf(pagesize);
		}
		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = "from t_product where userid=? order by createtime desc";
		whereArgs.add(userid);
		
		String limit = " limit " + (index-1)*size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" select count(*) " + sql , whereArgs.toArray());
		
		List<TProduct> promotions = JdbcUtil.queryForList(baseJdbcDao, TProduct.class, " select * " + sql + limit, whereArgs.toArray());
		
		Page page = new Page(promotions, index, totalCount, size);
		
		result.put("result", page);
		return result;
	}

}
